﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using MySqlX.XDevAPI.Relational;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CoreShop.App
{
    public partial class frmfoodtag : UIForm
    {
        public int remarkType = 1;
        private List<corecmsfoodtag> tagList = null;
        public frmfoodtag()
        {
            InitializeComponent();            
        }

        private void btnOk_Click(object sender, System.EventArgs e)
        {
            var bl = false;
            if (remarkType == 1)
            {
                (this.Owner as FOrdering).GetOrderingSelected();
                (this.Owner as FOrdering).orderingSelected.remark = txtRemark.Text;
                bl = new corecmsfoodorderingServices().Update((this.Owner as FOrdering).orderingSelected);
            }
            else {
                //(this.Owner as FOrdering).RefreshOrderingMaster((this.Owner as FOrdering).orderingSelected.orderId);
                //(this.Owner as FOrdering).orderingMaster.remark = txtRemark.Text;
                //bl = new corecmsfoodorderingmasterServices().Update((this.Owner as FOrdering).orderingMaster);
                bl = new corecmsfoodorderingServices().Update(a => new corecmsfoodordering()
                {
                    remark = txtRemark.Text,
                }, a => a.orderId == (this.Owner as FOrdering).orderingSelected.orderId);
            }
            if (!bl)
            {
                UIMessageBox.Show("保存失败。", "系统出错-SaveOrderingFeed:", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void frmfoodtask_Load(object sender, System.EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            if (remarkType == 0)
            {
                this.Text = "整单备注";
                lblFoodName.Text = "整单备注";
                pnlCaption.Text = "备注内容";
                txtRemark.Text = (this.Owner as FOrdering).orderingMaster.remark;
            }
            else
            {
                lblFoodName.Text = "商品：" + (this.Owner as FOrdering).orderingSelected.name;
                txtRemark.Text = (this.Owner as FOrdering).orderingSelected.remark;
            }
            tagList = new corecmsfoodtagServices().QueryAsync((int)GlobalEnumVars.FoodTag.OrderingTag);
            mbtRemarkTag.Clear();
            mbtRemarkTag.MaxSelected = 10;
            mbtRemarkTag.AutoWidth = true;
            foreach (var item in tagList)
            {
                mbtRemarkTag.Add(item.content, item.id);
            }
        }

        private void mbtGetRemark_SelectedChanged(object sender, EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            UIButton btn = button.SelectedBtn;

            if (btn != null)
            {
                string btnTxt = btn.Text;
                bool btnSelect = btn.Selected;
                if (btnSelect)
                {
                    if (!string.IsNullOrEmpty(txtRemark.Text)) txtRemark.Text = txtRemark.Text + ",";
                    txtRemark.Text = txtRemark.Text + btnTxt;
                }
                else
                {
                    txtRemark.Text = txtRemark.Text.ToString().Replace(btnTxt, "").Replace(",,", ",").TrimStart(',').TrimEnd(',');
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void pnlMaster_Click(object sender, EventArgs e)
        {
            if (pnlMaster.Left > 6)
            {
                pnlMaster.Left = 6;
                btnDiscount.Left = 80;
                pnlMaster.Text = "单品";
                btnDiscount.Text = "整单";
                remarkType = 0;//整单备注
            }
            else
            {
                pnlMaster.Left = 80;
                btnDiscount.Left = 6;
                pnlMaster.Text = "整单";
                btnDiscount.Text = "单品";
                remarkType = 1;//单品备注
            }
            LoadData();
        }

        private void txtRemark_Enter(object sender, EventArgs e)
        {
            if (AppConstVars.systemsetup.showKeyboard) txtRemark.ShowDropDown();
        }

        private void frmfoodtag_Shown(object sender, EventArgs e)
        {
            txtRemark.Focus();
        }
    }
}
